import React from 'react';


import DiseaseAddStyle from "../assets/css/DiseaseAdd.module.css";
import {Button, InputItem, List, Picker, TextareaItem, Toast,SearchBar} from "antd-mobile";

import {GetEnterVarietyList, GetOutSlaughterAddDetail, SaveSickness,} from "../http/Request";
// import {PhotoConsumer, PhotoProvider} from "react-photo-view";
import OutSlaughterAddDetailStyle from "../assets/css/OutSlaughterAddDetail.module.css";


// let filesData = [];
// let filesObj = {};

export default class EntryAddList extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            hasError: false,
            // files: filesData,
            leaveImageUrl: '',
            ageValue: '',
            weightValue: '',
            leavePrice: '',
            LivestockType: '',
            leaveSourceAddress: '',
            remarks: '',
            time: new Date().getTime(),
            leaveType: '1010',
            info: {},
            DoctorValue:'',
            DocPrice:'',
            seeImg:'',
            sValue:'',
            imageShow:false,
            searchValue:'',
        }
    }

    onErrorClick = () => {
        if (this.state.hasError) {
            Toast.info('不能为空');
        }
    }

    //获取医生
    onChangeDoctValue = (value) => {
        this.setState({
            DoctorValue: value,
            hasError: false,
        });
    }
    //获取价格
    onChangeDocPrice = (value) => {
        this.setState({
            DocPrice: value,
        });
    };

    //获取我的备注
    onChangeRemarks = (value) => {
        this.setState({
            remarks: value
        })
    }

    //获取信息
    getOutSlaughterAddDetail = () => {
        let param = {
            id: this.props.match.params['infoId']
        };
        GetOutSlaughterAddDetail(param).then(res => {
            this.setState({
                info: res.body,
                ageValue: res.body.leaveAge,
                weightValue: res.body.leaveWeight,
                LivestockType: res.body.enterVarietyName,
                leaveSourceAddress: res.body.leaveSourceAddress,
                seeImg:res.body.enterImages
            })
        }).catch(err => {

        })
    };

    //提交完成
    submitData() {
        let exp = parseInt(this.props.match.params['infoId'])
        if(!exp&& typeof(exp)!="undefined"&& exp!==0){
            console.log('1111');
            this.doneAddNull()

        }else{
            console.log('2222');
            this.doneAdd()
        }
    }

    doneAdd(){
        if(this.state.sValue[0] === undefined){
            Toast.offline('请选择种类', 1);
            return;
        }
        if(this.state.DoctorValue.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === '') {
            Toast.offline('请输入医生', 1);
            return;
        }
        if(this.state.DocPrice.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === ''){
            Toast.offline('请输入价格', 1);
            return;
        }
        if(this.state.remarks.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === ''){
            Toast.offline('请输入备注', 1);
            return;
        }

        let body = {
            infoId:parseInt(this.props.match.params['infoId']),
            enterVarietyId:this.state.sValue[0]+'',
            sicknessDoctor:parseInt(this.state.DoctorValue),
            sicknessPrice:parseInt(this.state.DocPrice),
            sicknessRemark:this.state.remarks,
        };

        SaveSickness(body).then(resolve => {
            console.log(resolve);
            Toast.info('添加完成');
        }).catch(err => {

        })
    }

    doneAddNull(){
        if(this.state.sValue[0] === undefined){
            Toast.offline('请选择种类', 1);
            return;
        }
        if(this.state.DoctorValue.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === '') {
            Toast.offline('请输入医生', 1);
            return;
        }
        if(this.state.DocPrice.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === ''){
            Toast.offline('请输入价格', 1);
            return;
        }
        if(this.state.remarks.replace(/(^\s*)|(\s*$)/g, '').replace(/[\r\n]/g, '') === ''){
            Toast.offline('请输入备注', 1);
            return;
        }
        let body = {
            infoId:parseInt(this.state.searchValue),
            enterVarietyId:this.state.sValue[0]+'',
            sicknessDoctor:parseInt(this.state.DoctorValue),
            sicknessPrice:parseInt(this.state.DocPrice),
            sicknessRemark:this.state.remarks,
        };

        SaveSickness(body).then(resolve => {
            console.log(resolve);
            Toast.info('添加完成');
        }).catch(err => {

        })
    }

    //查看入栏照片
    showBanner = () =>{
        let that = this;
        that.setState({
            imageShow:!that.state.imageShow
        })
    }

    //查看入栏照片
    ImageView =() => {
        return (
            <div className={OutSlaughterAddDetailStyle.modalBox} onClick={this.showBanner.bind(this)}>
                <img className={OutSlaughterAddDetailStyle.modalImage} src={this.state.seeImg} alt=""/>
            </div>

        );
    }

    /**
     * 获取搜索值
     * */
    getSearchValue(e) {
        this.setState({
            searchValue: e
        })
    }

    searchList(){
        let param = {
            id: this.state.searchValue
        };
        GetOutSlaughterAddDetail(param).then(res => {
            if(res.body.infoId == null){
                Toast.info('暂无数据');
            }else{
                this.setState({
                    info: res.body,
                    ageValue: res.body.leaveAge,
                    weightValue: res.body.leaveWeight,
                    LivestockType: res.body.enterVarietyName,
                    leaveSourceAddress: res.body.leaveSourceAddress,
                    seeImg:res.body.enterImages
                })
            }

        }).catch(err => {

        })
    }

    //搜索编号
    SearchNum() {
        return (
            <SearchBar
                placeholder="请输入编号"
                value={this.state.searchValue}
                cancelText={'搜索'}
                onCancel={this.searchList.bind(this)}
                onChange={this.getSearchValue.bind(this)}/>
        )

    }
    //获取种类
    GetLivestockType () {
        let body = {
            varietyType:'9003'
        };
        GetEnterVarietyList(body).then(resolve => {
            let Resolvearr = resolve.body
            let DataArr = []
            Resolvearr.forEach(element => {
                // console.log(element)
                let CurrentObj = {};
                CurrentObj['value'] = element.varietyId;
                CurrentObj['label'] = element.varietyName;
                DataArr.push(CurrentObj)
            });

            this.setState({
                TypeList:DataArr
            })
        }).catch(err => {

        })
    }

    //数据渲染完成-生命周期
    componentDidMount() {
        this.getOutSlaughterAddDetail()
        this.GetLivestockType()
    }

    render() {
        // const {files} = this.state;
        return (
            <div className={'main'}>
                {this.state.imageShow ? this.ImageView() : ''}
                {this.SearchNum()}
                <div className={DiseaseAddStyle.topPart}/>
                <div className={DiseaseAddStyle.buttonBox}>
                    <Button onClick={this.submitData.bind(this)} type="ghost" inline size="small">完成</Button>
                </div>
                <div style={{margin: '15px', fontSize: '16px'}}>
                    编号：{this.state.info['infoId']}
                </div>
                <div className={DiseaseAddStyle.InfoBoard}>
                    <div className={DiseaseAddStyle.rowList}>
                        <div className={DiseaseAddStyle.row}>
                            <div>
                                <label>入栏时间:</label><span>{this.state.info['enterTime']}</span>
                            </div>
                            <div>
                                <label>状态:</label><span>{this.state.info['leaveType'] === 1001 ? '入栏' : '出栏'}</span>
                            </div>
                        </div>
                        <div className={DiseaseAddStyle.row}>
                            <div>
                                <label>入栏年龄:</label><span>{this.state.info['enterAge']}个月</span>
                            </div>
                            <div>
                                <label>种类:</label><span>{this.state.info['enterVarietyName']}</span>
                            </div>
                        </div>
                        <div className={DiseaseAddStyle.row}>
                            <div>
                                <label>入栏价格:</label><span>¥{this.state.info['enterPrice']}</span>
                            </div>
                            <div>
                                <label>来源地:</label><span>{this.state.info['enterSourceAddress']}</span>
                            </div>
                        </div>
                        <div className={DiseaseAddStyle.row}>
                            <div>
                                <label>入栏体重:</label><span>{this.state.info['enterWeight']}kg</span>
                            </div>
                            <div>
                                <label>备注:</label><span>{this.state.info['remarks']}</span>
                            </div>
                        </div>
                    </div>
                </div>
                <div className={DiseaseAddStyle.buttonBox}>
                    <Button onClick={this.showBanner.bind(this)} style={{marginRight: '15px'}}  type="ghost" inline size="small">查看入栏照片</Button>
                </div>
                <div className={DiseaseAddStyle.formBox}>
                    <List className={DiseaseAddStyle.formList}>

                        <Picker data={this.state.TypeList} cols={1}
                                value={this.state.sValue}
                                onChange={v => this.setState({ sValue: v })}
                                onOk={v => this.setState({ sValue: v })}
                        >
                            <List.Item arrow="horizontal">疾病种类</List.Item>
                        </Picker>
                        <InputItem type={"number"} onChange={this.onChangeDoctValue}
                                   value={this.state.DoctorValue}>医生</InputItem>
                        <InputItem type={"number"} onChange={this.onChangeDocPrice}
                                   value={this.state.DocPrice}>价格</InputItem>
                        <TextareaItem defaultValue={'填写备注'}
                                      style={{height: '100px', border: '1px solid #fefefe', padding: '5px'}}
                                      title="备注" onChange={this.onChangeRemarks} value={this.state.remarks}/>

                    </List>
                </div>
            </div>
        )
    }
}